package com.orange.score.activity.entry;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;
import com.orange.common.android.widget.PullToRefreshExpandableListView;
import com.orange.common.android.widget.PullToRefreshExpandableListView.OnRefreshListener;
import com.orange.score.activity.common.CommonFootballActivity;
import com.orange.score.activity.index.SelectCompanyActivity;
import com.orange.score.activity.league.SelectLeagueActivity;
import com.orange.score.app.ScoreApplication;
import com.orange.score.constants.ScoreType;
import com.orange.score.model.index.CompanyManager;
import com.orange.score.model.index.Odds;
import com.orange.score.model.index.OddsGroup;
import com.orange.score.model.index.OddsManager;
import com.orange.score.model.league.League;
import com.orange.score.model.league.LeagueManager;
import com.orange.score.model.match.Match;
import com.orange.score.model.match.MatchManager;
import com.orange.score.network.ResultCodeType;
import com.orange.score.service.IndexService;
import com.orange.score.service.IndexServiceCallBack;
import com.orange.score.service.OddsUpdateChangeCallBack;
import com.orange.score.utils.DateUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TimeZone;

public class RealtimeIndexActivity2 extends CommonFootballActivity
  implements IndexServiceCallBack, ExpandableListView.OnGroupClickListener, OddsUpdateChangeCallBack
{
  public static String DATE_FORMAT = "yyyy-MM-dd";
  private static final String TAG = "RealtimeIndexActivity";
  private RealtimeIndexOddsListAdapter adapter;
  List<League> allLeagues;
  Button buttonFilterCompany;
  Button buttonFilterLeague;
  Button buttonFilterScoreType;
  Button buttonLookBack;
  String companyId;
  CompanyManager companyManager;
  int currentDateIndex = 0;
  String date;
  CharSequence[] dateList;
  boolean firstLoad;
  private List<OddsGroup> groups;
  IndexService indexService;
  LeagueManager leagueManager;
  private PullToRefreshExpandableListView listView;
  MatchManager matchManager;
  Map<Match, List<Odds>> matchOddsMap;
  TextView noOddsTextView;
  OddsManager oddsManager;
  Set<String> selectedSet;
  List<League> topLeagues;

  public RealtimeIndexActivity2()
  {
    this.companyId = null;
    this.matchOddsMap = new HashMap();
    this.allLeagues = new ArrayList();
    this.topLeagues = new ArrayList();
    this.selectedSet = new HashSet();
    this.firstLoad = true;
  }

  private int buttonIndexFromScoreType(ScoreType paramScoreType)
  {
    int i;
    switch ($SWITCH_TABLE$com$orange$score$constants$ScoreType()[paramScoreType.ordinal()])
    {
    default:
      i = 0;
    case 2:
    case 1:
    case 3:
    case 4:
    case 5:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return i;
              i = 0;
            }
            i = 1;
          }
          i = 2;
        }
        i = 3;
      }
      i = 4;
    }
  }

  private ScoreType buttonIndexToScoreType(int paramInt)
  {
    ScoreType localScoreType;
    switch (paramInt)
    {
    default:
      localScoreType = ScoreType.FIRST;
    case 1:
    case 2:
    case 3:
    case 4:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return localScoreType;
            localScoreType = ScoreType.ALL;
          }
          localScoreType = ScoreType.ZUCAI;
        }
        localScoreType = ScoreType.JINGCAI;
      }
      localScoreType = ScoreType.DANCHANGE;
    }
  }

  private CharSequence[] getDateList()
  {
    String[] arrayOfString = new String[7];
    Calendar localCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+0800"));
    localCalendar.setTime(new Date());
    int i = 0;
    if (i >= arrayOfString.length)
    {
      this.dateList = arrayOfString;
      return arrayOfString;
    }
    if (i == 0)
      localCalendar.add(5, 0);
    while (true)
    {
      while (true)
      {
        arrayOfString[i] = DateUtil.dateToStringByFormat(localCalendar.getTime(), DATE_FORMAT);
        ++i;
      }
      localCalendar.add(5, -1);
    }
  }

  private IndexService getIndexService()
  {
    return ((ScoreApplication)getApplication()).getIndexService();
  }

  private MatchManager getMatchManager()
  {
    return getIndexService().getMatchManager();
  }

  private String getScoreTypeString(ScoreType paramScoreType)
  {
    String str;
    switch ($SWITCH_TABLE$com$orange$score$constants$ScoreType()[paramScoreType.ordinal()])
    {
    case 2:
    default:
      str = getResources().getString(2131361830);
    case 1:
    case 4:
    case 5:
    case 3:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return str;
            str = getResources().getString(2131361831);
          }
          str = getResources().getString(2131361834);
        }
        str = getResources().getString(2131361832);
      }
      str = getResources().getString(2131361833);
    }
  }

  public static Map.Entry[] getSortedMapEntry(Map paramMap)
  {
    Set localSet = paramMap.entrySet();
    Map.Entry[] arrayOfEntry = (Map.Entry[])localSet.toArray(new Map.Entry[localSet.size()]);
    Arrays.sort(arrayOfEntry, new Comparator()
    {
      public int compare(, Object paramObject2)
      {
        return ((Match)((Map.Entry)paramObject1).getKey()).compareTo((Match)((Map.Entry)paramObject2).getKey());
      }
    });
    return arrayOfEntry;
  }

  private void initListView()
  {
    this.listView = ((PullToRefreshExpandableListView)findViewById(2131558518));
    this.groups = new ArrayList();
    this.adapter = new RealtimeIndexOddsListAdapter(this, this.groups, this.companyManager);
    this.listView.setAdapter(this.adapter);
    this.listView.setOnGroupClickListener(this);
    this.listView.setFastScrollEnabled(true);
    this.listView.setDividerHeight(0);
    this.listView.setOnRefreshListener(new PullToRefreshExpandableListView.OnRefreshListener(this)
    {
      public void onRefresh()
      {
        Log.d("RealtimeIndexActivity", "pull to refresh!");
        RealtimeIndexActivity2.access$0(this.this$0);
      }
    });
  }

  private void loadAllOddsData()
  {
    this.indexService.loadOddsData(this, this, this.date, this.companyId);
  }

  private void loadCompanyData()
  {
    this.indexService.loadCompanyData(this, this);
  }

  private void reloadListDataAndView()
  {
    ArrayList localArrayList;
    Iterator localIterator;
    if (this.firstLoad)
    {
      localArrayList = new ArrayList();
      localIterator = this.topLeagues.iterator();
    }
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        this.oddsManager.setFilterLeagueIdList(localArrayList);
        this.leagueManager.setLeagueList(this.topLeagues);
        HashSet localHashSet = new HashSet();
        localHashSet.addAll(localArrayList);
        this.leagueManager.setFilterLeagueSet(localHashSet);
        this.firstLoad = false;
        this.matchOddsMap = this.oddsManager.filterOddsByOddsType(this.companyManager.getOddsType());
        updateMatchOddsMap();
        updateNoOddsTextView();
        this.adapter.notifyDataSetChanged();
        return;
      }
      localArrayList.add(((League)localIterator.next()).getLeagueId());
    }
  }

  private void selectAllLeagues()
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.allLeagues.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        HashSet localHashSet = new HashSet();
        localHashSet.addAll(localArrayList);
        this.leagueManager.setLeagueList(this.allLeagues);
        this.leagueManager.setFilterLeagueSet(localHashSet);
        this.oddsManager.setFilterLeagueIdList(localArrayList);
        return;
      }
      localArrayList.add(((League)localIterator.next()).getLeagueId());
    }
  }

  private void selectTopLeagues()
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.topLeagues.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        HashSet localHashSet = new HashSet();
        localHashSet.addAll(localArrayList);
        this.leagueManager.setLeagueList(this.topLeagues);
        this.leagueManager.setFilterLeagueSet(localHashSet);
        this.oddsManager.setFilterLeagueIdList(localArrayList);
        return;
      }
      localArrayList.add(((League)localIterator.next()).getLeagueId());
    }
  }

  private void setCompanyIdFromList(List<String> paramList)
  {
    this.companyId = paramList.toString();
    this.companyId = this.companyId.substring(1, this.companyId.length() - 1).replace(" ", "");
    Log.d("RealtimeIndexActivity", "load Odds data of company: " + this.companyId);
  }

  private void setFilterCompanyButtonOnClickListener()
  {
    this.buttonFilterCompany.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.selectedSet.clear();
        Intent localIntent = new Intent();
        localIntent.setClass(this.this$0, SelectCompanyActivity.class);
        this.this$0.startActivityForResult(localIntent, 20120217);
      }
    });
  }

  private void setFilterLeagueButtonOnClickListener()
  {
    this.buttonFilterLeague.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        Intent localIntent = new Intent();
        localIntent.setClass(this.this$0, SelectLeagueActivity.class);
        Bundle localBundle = new Bundle();
        localBundle.putInt("league_from", 2);
        ArrayList localArrayList = new ArrayList();
        localArrayList.addAll(this.this$0.leagueManager.getFilterLeagueSet());
        localBundle.putStringArrayList("selected_league", localArrayList);
        localIntent.putExtras(localBundle);
        this.this$0.startActivityForResult(localIntent, 20120109);
      }
    });
  }

  private void setFilterLevelButtonOnClickListener()
  {
    this.buttonFilterScoreType.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.selectedSet.clear();
        int i = RealtimeIndexActivity2.access$2(this.this$0, this.this$0.oddsManager.getFilterScoreType());
        new AlertDialog.Builder(this.this$0).setTitle("请选择赛事类型").setSingleChoiceItems(2131165185, i, new DialogInterface.OnClickListener(this)
        {
          public void onClick(, int paramInt)
          {
            Log.d("RealtimeIndexActivity", "click " + paramInt);
            paramDialogInterface.dismiss();
            ScoreType localScoreType = RealtimeIndexActivity2.access$3(RealtimeIndexActivity2.2.access$0(this.this$1), paramInt);
            if (localScoreType != RealtimeIndexActivity2.2.access$0(this.this$1).oddsManager.getFilterScoreType())
            {
              RealtimeIndexActivity2.2.access$0(this.this$1).oddsManager.setFilterScoreType(localScoreType);
              RealtimeIndexActivity2.access$4(RealtimeIndexActivity2.2.access$0(this.this$1));
              RealtimeIndexActivity2.access$0(RealtimeIndexActivity2.2.access$0(this.this$1));
            }
          }
        }).setNegativeButton(2131361823, new DialogInterface.OnClickListener(this)
        {
          public void onClick(, int paramInt)
          {
            Log.d("RealtimeIndexActivity", "click cancel");
          }
        }).create().show();
      }
    });
  }

  private void setLookBackButtonOnClickListener()
  {
    this.buttonLookBack.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.selectedSet.clear();
        new AlertDialog.Builder(this.this$0).setTitle("历史回查").setSingleChoiceItems(RealtimeIndexActivity2.access$5(this.this$0), this.this$0.currentDateIndex, new DialogInterface.OnClickListener(this)
        {
          public void onClick(, int paramInt)
          {
            Log.d("RealtimeIndexActivity", "click " + paramInt);
            paramDialogInterface.dismiss();
            RealtimeIndexActivity2.5.access$0(this.this$1).currentDateIndex = paramInt;
            RealtimeIndexActivity2.5.access$0(this.this$1).date = ((String)RealtimeIndexActivity2.5.access$0(this.this$1).dateList[paramInt]);
            RealtimeIndexActivity2.access$0(RealtimeIndexActivity2.5.access$0(this.this$1));
          }
        }).setNegativeButton(2131361823, new DialogInterface.OnClickListener(this)
        {
          public void onClick(, int paramInt)
          {
            Log.d("RealtimeIndexActivity", "click cancel");
          }
        }).create().show();
      }
    });
  }

  private void updateNoOddsTextView()
  {
    if (this.groups.size() > 0)
      this.noOddsTextView.setVisibility(8);
    while (true)
    {
      return;
      this.noOddsTextView.setVisibility(0);
    }
  }

  private void updateScoreTypeButton()
  {
    ((Button)findViewById(2131558514)).setText(getScoreTypeString(this.oddsManager.getFilterScoreType()));
  }

  public void loadAllCompanyFinish(ResultCodeType paramResultCodeType)
  {
    switch ($SWITCH_TABLE$com$orange$score$network$ResultCodeType()[paramResultCodeType.ordinal()])
    {
    default:
    case 1:
    }
    while (true)
    {
      return;
      setCompanyIdFromList(this.companyManager.getDefaultSelectedCompanyIdList());
      loadAllOddsData();
      this.listView.onRefreshComplete();
    }
  }

  public void loadOddsDataFinish(ResultCodeType paramResultCodeType)
  {
    switch ($SWITCH_TABLE$com$orange$score$network$ResultCodeType()[paramResultCodeType.ordinal()])
    {
    default:
      return;
    case 1:
    }
    this.topLeagues = this.leagueManager.findAllTopLeagues();
    this.allLeagues = this.leagueManager.findAllLeagues();
    if ((this.selectedSet != null) && (this.selectedSet.size() > 0))
    {
      ArrayList localArrayList = new ArrayList();
      localArrayList.addAll(this.selectedSet);
      this.oddsManager.setFilterLeagueIdList(localArrayList);
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                  reloadListDataAndView();
                switch ($SWITCH_TABLE$com$orange$score$constants$ScoreType()[this.oddsManager.getFilterScoreType().ordinal()])
                {
                default:
                case 1:
                case 2:
                case 5:
                case 4:
                case 3:
                }
              }
              selectAllLeagues();
            }
            selectTopLeagues();
          }
          selectAllLeagues();
        }
        selectAllLeagues();
      }
      selectAllLeagues();
    }
  }

  public void notifyOddsUpdate(Set<Odds> paramSet)
  {
    Log.d("RealtimeIndexActivity", "notifyOddsUpdate");
    if ((paramSet == null) || (paramSet.size() == 0));
    while (true)
    {
      return;
      reloadListDataAndView();
    }
  }

  protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent)
  {
    Log.d("RealtimeIndexActivity", "onActivityResult");
    super.onActivityResult(paramInt1, paramInt2, paramIntent);
    if (paramIntent == null)
      Log.d("RealtimeIndexActivity", "onActivityResult but data is null");
    while (true)
    {
      ArrayList localArrayList1;
      while (true)
      {
        while (true)
        {
          ArrayList localArrayList2;
          while (true)
          {
            while (true)
            {
              return;
              switch (paramInt1)
              {
              default:
              case 20120109:
              case 20120217:
              }
            }
            localArrayList2 = paramIntent.getStringArrayListExtra("selected_league");
            if (localArrayList2 != null)
              break;
            Log.d("RealtimeIndexActivity", "onActivityResult but select league list is null");
          }
          Log.d("RealtimeIndexActivity", "onActivityResult, select league list is " + localArrayList2);
          this.selectedSet.clear();
          this.selectedSet.addAll(localArrayList2);
          this.leagueManager.setFilterLeagueSet(this.selectedSet);
          this.oddsManager.setFilterLeagueIdList(localArrayList2);
          reloadListDataAndView();
        }
        localArrayList1 = paramIntent.getStringArrayListExtra("selectedCompanyId");
        if (localArrayList1 != null)
          break;
        Log.d("RealtimeIndexActivity", "onActivityResult but select company list is null");
      }
      Log.d("RealtimeIndexActivity", "onActivityResult, select company list is " + localArrayList1);
      setCompanyIdFromList(localArrayList1);
      loadAllOddsData();
      reloadListDataAndView();
    }
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(2130903083);
    this.matchManager = getMatchManager();
    this.indexService = getIndexService();
    this.oddsManager = this.indexService.getOddsManager();
    this.companyManager = this.indexService.getCompanyManager();
    this.leagueManager = this.indexService.getLeagueManager();
    this.date = DateUtil.dateToStringByFormat(new Date(), DATE_FORMAT);
    loadCompanyData();
    this.buttonFilterCompany = ((Button)findViewById(2131558515));
    this.buttonFilterLeague = ((Button)findViewById(2131558516));
    this.buttonLookBack = ((Button)findViewById(2131558517));
    this.buttonFilterScoreType = ((Button)findViewById(2131558514));
    setFilterCompanyButtonOnClickListener();
    setFilterLeagueButtonOnClickListener();
    setLookBackButtonOnClickListener();
    setFilterLevelButtonOnClickListener();
    initListView();
    this.noOddsTextView = ((TextView)findViewById(2131558519));
    updateNoOddsTextView();
    this.indexService.addOddsLiveUpdateObserver(this);
  }

  public boolean onGroupClick(ExpandableListView paramExpandableListView, View paramView, int paramInt, long paramLong)
  {
    return false;
  }

  protected void onPause()
  {
    super.onPause();
    ScoreApplication.currentActivity = null;
  }

  public void onRefresh()
  {
    super.onRefresh();
    loadAllOddsData();
  }

  protected void onResume()
  {
    super.onResume();
    this.listView.onRefreshComplete();
    ScoreApplication.currentActivity = this;
  }

  public void updateMatchOddsMap()
  {
    int j;
    int k;
    this.groups.clear();
    Map.Entry[] arrayOfEntry = getSortedMapEntry(this.matchOddsMap);
    int i = 0;
    if (i >= arrayOfEntry.length)
    {
      j = this.adapter.getGroupCount();
      k = 0;
    }
    while (true)
    {
      if (k >= j)
      {
        return;
        Match localMatch = (Match)arrayOfEntry[i].getKey();
        if (!(this.oddsManager.canDisplayOdds(localMatch, this.date)));
        while (true)
        {
          while (true)
            ++i;
          OddsGroup localOddsGroup = new OddsGroup(localMatch, (List)arrayOfEntry[i].getValue());
          this.groups.add(localOddsGroup);
        }
      }
      this.listView.expandGroup(k);
      ++k;
    }
  }
}